var tpage = {
    page: function (totalRow, listRow, nowPage, $page) {
        if ((totalRow == 0 || listRow == 0) || nowPage == 0)
            return;
        tpage.pages = 5;
        //实际页数
        var s_pages = Math.ceil(totalRow / listRow);
        //间距页数
        if (tpage.pages > s_pages)
            tpage.pages = s_pages;
        var y_page = Math.floor(tpage.pages / 2);
        if (s_pages >= tpage.pages) {
            if (nowPage + y_page >= s_pages) {
                var firstPage = tpage.pages % 2 ? s_pages - 2 * y_page : s_pages - 2 * y_page + 1;
                if (firstPage < 1)
                    firstPage = 1;
                var endPage = s_pages;
            } else {
                var firstPage = tpage.pages % 2 ? nowPage - y_page : nowPage - y_page + 1;
                if (firstPage < 1) {
                    firstPage = 1;
                    var endPage = tpage.pages % 2 ? 2 * y_page + 1 : 2 * y_page;
                    if (endPage > s_pages)
                        endPage = s_pages;
                } else {
                    var endPage = tpage.pages % 2 ? firstPage + 2 * y_page : firstPage + 2 * y_page - 1;

                    if (endPage > s_pages)
                        endPage = s_pages;
                }
            }
        } else {
            var firstPage = 1, endPage = tpage.pages;
        }

        var listrowhtml = '<label>分页:<select name="page">';
        if (listRow == 20) {
            listrowhtml += '<option value="20" selected>20</option><option value="50">50</option><option value="100">100</option></select></label>';
        } else if (listRow == 50) {
            listrowhtml += '<option value="20">20</option><option value="50" selected>50</option><option value="100">100</option></select></label>';
        } else {
            listrowhtml += '<option value="20">20</option><option value="50">50</option><option value="100" selected>100</option></select></label>';
        }
        if (nowPage > 1)
            var node = '<a class="first ui-corner-tl ui-corner-bl fg-button ui-button ui-state-default" href="1">第一页</a><a class="previous fg-button ui-button ui-state-default" href="' + (nowPage - 1) + '">上一页</a><span>';
        else
            var node = '<a class="first ui-corner-tl ui-corner-bl fg-button ui-button ui-state-default ui-state-disabled">第一页</a><a class="previous fg-button ui-button ui-state-default ui-state-disabled" >上一页</a><span>';
        for (var i = firstPage; i <= endPage; i++) {
            if (i == nowPage)
                node += '<a class="fg-button ui-button ui-state-default ui-state-disabled">' + i + '</a>';
            else
                node += '<a class="fg-button ui-button ui-state-default" href="' + i + '">' + i + '</a>';
        }
        if (nowPage < s_pages)
            node += '</span><a class="next fg-button ui-button ui-state-default" href="' + (nowPage + 1) + '">下一页</a><a class="last ui-corner-tr ui-corner-br fg-button ui-button ui-state-default" href="' + s_pages + '">最后一页</a>';
        else
            node += '</span><a class="next fg-button ui-button ui-state-default ui-state-disabled">下一页</a><a class="last ui-corner-tr ui-corner-br fg-button ui-button ui-state-default ui-state-disabled">最后一页</a>';
        $page.data("btd.layer.enter", 0);
        $page.each(function () {
            $(this).children(".dataTables_filter").html(listrowhtml);
            $(this).children(".dataTables_paginate").html(node);

        })
        tpage.reselec($('body'));
    },
    reselec: function (t) {
        var $t = $(t);
        $t.each(function () {
            if ($(this).children())
                tpage.reselec($(this).children());
            if ($(this).attr("page"))
                $(this).attr("value", $($(this).attr("page")).find("option:selected").attr("value"));
        })
    }
}
